package com.acce.gxsc.page.registerCarRecord;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import com.acce.gxsc.R;
import com.acce.gxsc.model.bean.PregisterBillStateEvent;
import com.acce.gxsc.model.loaclBean.CompanyTitle;
import com.acce.gxsc.page.registerCarRecord.child.RegisterCarRecordChildFragment;
import com.acce.sdk.base.mvp.BaseMvpFragment;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by huxley on 2017/5/26.
 */

public class RegisterCarRecordFragment extends BaseMvpFragment<RegisterCarRecordView, RegisterCarRecordPresenter> implements RegisterCarRecordView {

    boolean isSelect;
    View    rootView;
//    @Bind(R.id.rv_goods_prediction)
//    RecyclerView mRvGoodsPrediction;

    //    CommonAdapter<CarPredictionBean> mCommonAdapter;
//    List<CarPredictionBean>          data;
    @Bind(R.id.tabs)
    TabLayout mTabs;
    @Bind(R.id.viewpager)
    ViewPager mViewpager;
    @Bind(R.id.cb_in)
    CheckBox  mCbIn;

    public static RegisterCarRecordFragment newInstance(boolean isSelect) {
        Bundle args = new Bundle();
        args.putBoolean("isSelect", isSelect);
        RegisterCarRecordFragment fragment = new RegisterCarRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected RegisterCarRecordPresenter createPresenter() {
        return new RegisterCarRecordPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_register_car_record, container, false);
        ButterKnife.bind(this, rootView);
        init();
//        mPresenter.loadRegisterCarInfo(null, null);
        return rootView;
    }

    private void init() {
        isSelect = getArguments().getBoolean("isSelect");

        Adapter adapter = new Adapter(getChildFragmentManager());
        adapter.createFragment(isSelect, new CompanyTitle("", "全部公司"));
        adapter.createFragment(isSelect, new CompanyTitle("兴利", "兴利"));
        adapter.createFragment(isSelect, new CompanyTitle("广进", "广进"));
        adapter.createFragment(isSelect, new CompanyTitle("健诚", "健诚"));
        adapter.createFragment(isSelect, new CompanyTitle("华隆", "华隆"));
        adapter.createFragment(isSelect, new CompanyTitle("其他", "其他"));
        mViewpager.setAdapter(adapter);
        mTabs.setupWithViewPager(mViewpager);
        mViewpager.setOffscreenPageLimit(adapter.getCount());
        mCbIn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                String type = b ? "0" : "";
                EventBus.getDefault().post(new PregisterBillStateEvent(type, query));
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    String query = "";
    public void find(String query) {
        mViewpager.setCurrentItem(0);
        String type = mCbIn.isChecked() ? "0" : "";
        this.query = query;
        EventBus.getDefault().post(new PregisterBillStateEvent(type, query));
    }

    public String getNo() {
        return query;
    }

    public String getType() {
        return mCbIn.isChecked() ? "0" : "";
    }

    private static class Adapter extends FragmentPagerAdapter {
        private final List<BaseMvpFragment> mFragments      = new ArrayList<>();
        private final List<String>          mFragmentTitles = new ArrayList<>();

        private Adapter(FragmentManager fm) {
            super(fm);
        }

        private void addFragment(BaseMvpFragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public BaseMvpFragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }

        private void createFragment(boolean isSelect, CompanyTitle cb) {
            RegisterCarRecordChildFragment newMovieFragment = RegisterCarRecordChildFragment.newInstance(isSelect, cb.name);
            addFragment(newMovieFragment, cb.title);
        }
    }
}
